package com.treasure.mall.biz.repository.user;

import com.treasure.mall.biz.entity.user.UserRelax;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.repository.user
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/11 14:03
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.biz.repository.user Inc. All rights reserved.
 */
public interface UserRelaxRepository extends JpaRepository<UserRelax, Long> {


    /**
     * 删除某个节点所有上级关联信息
     *
     * @param childId
     */
    @Query("delete from UserRelax ur where ur.childId=?1")
    @Modifying(clearAutomatically = true)
    @Transactional(rollbackFor = RuntimeException.class)
    void deleteByChild(Long childId);

    /**
     * 获取团队数量
     *
     * @param userId
     * @return
     */
    @Query("select count(ur.id) from  UserRelax  ur where ur.parent.userId=?1 and ur.depth=1")
    long countByUserId(Long userId);

}
